package com.ctg.itrdc.k8sconsole.control;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.K8sNamespaceDTO;
import com.ctg.itrdc.imageconsole.data.entity.K8sNamespace;
import com.ctg.itrdc.imageconsole.service.IK8sNamespaceService;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;

/**
 * 命名空间（多租户）相关接口
 * @author wupiao
 *
 */
@Controller
@RequestMapping("/namespace")
public class K8sNamespaceController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private IK8sNamespaceService k8sNamespaceService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult list(@RequestParam(value = "clusterCode") String clusterCode,
			@RequestParam(value = "namespaceName", required = false) String namespaceName,
			@RequestParam(value = "namespaceCode", required = false) String namespaceCode,
			@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
		try {
			K8sNamespaceDTO namespaceDTO = new K8sNamespaceDTO();
			namespaceDTO.setClusterCode(clusterCode);
			namespaceDTO.setNamespaceName(namespaceName);
			namespaceDTO.setNamespaceCode(namespaceCode);
			
			PageInfo<K8sNamespaceDTO> pageInfo = k8sNamespaceService.queryNamespaceList(namespaceDTO,  pageNum,
					pageSize);
			if (pageInfo.getList().size() > 0) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
			}
			PageInfo<K8sNamespace> pageInfoForNull = new PageInfo<>();
			pageInfoForNull.setList(new ArrayList<K8sNamespace>());

			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfoForNull);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}
	
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult list(@RequestParam("id") Long id) {
		try {
			K8sNamespace k8sNamespace = k8sNamespaceService.queryNamespaceById(id);

			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, k8sNamespace);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}
	
	
	@RequestMapping(value = "/combineJson", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult combineJson(@RequestBody K8sNamespace k8sNamespace){
		try {
			List<Object> list = k8sNamespaceService.combineJson(k8sNamespace);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult create(@RequestBody K8sNamespace k8sNamespace) {
		try {
			k8sNamespaceService.createNamepace(k8sNamespace);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult update(@RequestBody K8sNamespace k8sNamespace) {
		try {
			k8sNamespaceService.updateNamepace(k8sNamespace);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/checkIsExist", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult checkIsExist(@RequestParam(value = "clusterCode") String clusterCode,
			@RequestParam(value = "namespaceCode", required = false) String namespaceCode){
		try {
			boolean isExist = k8sNamespaceService.checkIsExist(clusterCode, namespaceCode);
			// 返回true代表存在，false代表不存在 
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, isExist);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
		
	}
	 
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult delete(@RequestBody K8sNamespace K8sNamespace) {
		try {
			k8sNamespaceService.deleteNamepace(K8sNamespace);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

}
